<?php
require_once("myheader.php");
require_once("myfooter.php");
include("helper/connexion.php");
include("helper/enseignement_helper.php");
connexion_sql();
myHeader("enseignements");

if(isset($_GET['action']) &&  !empty($_GET['action']))
{
  $action = $_GET['action'];
  if($action == 'edit' &&  isset($_GET['id'])) {
     $id = (int) $_GET['id'];
	// $data = pg_fetch_assoc(getOptionById($id)); 
   }
	
	if($action == 'creation' || $action = 'edit'){
		if(isset($_POST['inputName']) && isset($_POST['inputNbTD']) && isset($_POST['inputPromo']) && isset($_POST['inputFormation'])){
				if($action == 'creation'){
					ajoutOption($_POST['inputName'],$_POST['inputNbTD'],$_POST['inputPromo'],$_POST['inputFormation']);
					echo "<h2> Option ajoutée </h2>";
				}else{
					updateOption($id,$_POST['inputName'],$_POST['inputNbTD'],$_POST['inputPromo'],$_POST['inputFormation']);
					echo "<h2> Option modifiée </h2>";
					$data = pg_fetch_assoc(getOptionById($id)); 
				}
		}else{
			echo "<h2> Donnée manquante </h2>";
		}
		
	}
}
if(isset($_GET['id'])) {
	 $id = (int) $_GET['id'];
		 $data = pg_fetch_assoc(getOptionById($id)); 
}	

//suppressionEnseignant($id);
?>
<h2><?php echo (isset($_GET['id'])) ? 'Modifier' : 'Ajouter';?> une option</h2>
<form class="form-horizontal" method="post" action="gestion_option.php?action=<?php echo (isset($_GET['id'])) ? 'edit&id='.$id:'creation'; ?>">
  <div class="control-group">
    <label class="control-label" for="inputName">Nom</label>
    <div class="controls">
      <input type="text" id="inputName" name="inputName"  value="<?php echo ((isset($data['nomoption'])) ? $data['nomoption'] : '');  ?>">
    </div>
  </div>
   <div class="control-group">
    <label class="control-label" for="inputName">Nombre de TD</label>
    <div class="controls">
      <input type="text" id="inputNbTD" name="inputNbTD"  value="<?php echo ((isset($data['nbgroupestd'])) ? $data['nbgroupestd'] : '');  ?>">
    </div>
  </div>  
  <div class="control-group">
    <label class="control-label" for="inputPromo">Promo</label>
    <div class="controls">
      <input type="text" id="inputPromo" name="inputPromo"  value="<?php echo ((isset($data['promo'])) ? $data['promo'] : '');  ?>">
    </div>
  </div>
  <div class="control-group">
     <label class="control-label" for="inputFormation">Formation</label>
     <div class="controls">
		<select name="inputFormation" id="inputFormation"/>
	<?php
		$req = getFormation();
		while($row = pg_fetch_assoc($req))
		{
				echo '<option name="inputFormation" value="'.$row["idformation"].'"';
				echo ((isset($data["inputFormation"]) && $data["inputFormation"]==$row["idformation"]) ? "selected" : "");
				echo ' >'.$row["nomformation"].'</option>';
		}
	?>
		</select>
	 </div>
</div>
  <div class="form-actions">
     	  <button type="submit" class="btn btn-primary" name="valid">Valider</button>
     </div>  
</form>

<?php
myFooter();
?>